home *** CD-ROM | disk | FTP | other *** search
- unit Exinidb0;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, AdIniDB, Buttons, AdMisc, AdExcept;
-
- type
- TDataRecord = record
- Name : String[21];
- Age : Integer;
- end;
-
- type
- TForm1 = class(TForm)
- Records: TGroupBox;
- ListBox1: TListBox;
- Label1: TLabel;
- Label2: TLabel;
- Edit1: TEdit;
- Edit2: TEdit;
- BitBtn1: TBitBtn;
- BitBtn2: TBitBtn;
- BitBtn3: TBitBtn;
- IniDBase1: TApdIniDBase;
- procedure FormCreate(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- procedure BitBtn3Click(Sender: TObject);
- procedure ListBox1Click(Sender: TObject);
- procedure BitBtn2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure ValidateEntry(var Rec : TDataRecord);
- {-Make sure everything entered in the TEdit's is valid}
- procedure UpdateEdits;
- {-Update the data in the TEdits}
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.ValidateEntry(var Rec : TDataRecord);
- {-Make sure everything entered in the TEdit's is valid}
- var
- E : Integer;
- NameStr : String;
- AgeStr : String;
-
- begin
- {remove trailing spaces from input data}
- NameStr := TrimTrail(Edit1.Text);
- AgeStr := TrimTrail(Edit2.Text);
-
- {make sure the user entered a name}
- if (NameStr = '') then begin
- Edit1.SetFocus;
- raise Exception.Create('You must enter a name');
- end;
-
- {make sure the user entered an age}
- if (AgeStr = '') then begin
- Edit2.SetFocus;
- raise Exception.Create('You must enter an age');
- end;
-
- {validate the entered age--make sure it's really a number}
- Val(Edit2.Text, Rec.Age, E);
- if (E <> 0) then begin
- Edit2.SetFocus;
- raise Exception.Create('Invalid age');
- end;
-
- Rec.Name := Edit1.Text;
- end;
-
- procedure TForm1.UpdateEdits;
- {-Update the data in the TEdits}
- var
- Rec : TDataRecord;
-
- begin
- if (ListBox1.ItemIndex <> -1) then begin
- {read the selected record from the database}
- IniDBase1.GetRecord(ListBox1.Items[ListBox1.ItemIndex], Rec);
-
- {update the TEdits on the form with the new data}
- Edit1.Text := Rec.Name;
- Edit2.Text := IntToStr(Rec.Age);
- end else begin
- {blank the TEdits}
- Edit1.Text := '';
- Edit2.Text := '';
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- {put the initial list of records in the listbox}
- Listbox1.Items := IniDBase1.RecordList;
-
- {select the first item in the listbox. if there are any records available}
- if (ListBox1.Items.Count <> 0) then begin
- ListBox1.ItemIndex := 0;
- UpdateEdits;
- end;
- end;
-
- procedure TForm1.BitBtn1Click(Sender: TObject);
- var
- Rec : TDataRecord;
-
- begin
- {make sure the user has enetered valid data}
- ValidateEntry(Rec);
-
- {add the record to the database}
- try
- {put the record in the file}
- IniDBase1.AddRecord(Rec);
-
- {put the new record's name in the listbox and highlight it}
- ListBox1.Items.Add(Rec.Name);
- ListBox1.ItemIndex := Pred(ListBox1.Items.Count);
- except
- {if the record already exists, let the user know}
- on ERecordExists do
- MessageDlg('A record with that name already exists', mtError, [mbOK], 0);
- else
- raise;
- end;
- end;
-
- procedure TForm1.BitBtn2Click(Sender: TObject);
- var
- Idx : Integer;
- Rec : TDataRecord;
-
- begin
- {make sure something in the listbox is selected}
- Idx := ListBox1.ItemIndex;
- if (Idx = -1) then begin
- MessageBeep(0);
- Exit;
- end;
-
- {get input data}
- ValidateEntry(Rec);
-
- {change the record in the file}
- IniDBase1.UpdRecord(ListBox1.Items[Idx], Rec);
-
- {change the text in the listbox, if necessary, and re-highlight the record}
- ListBox1.Items[Idx] := Rec.Name;
- ListBox1.ItemIndex := Idx;
- end;
-
- procedure TForm1.BitBtn3Click(Sender: TObject);
- var
- Idx : Integer;
- NewIdx : Integer;
- Rec : TDataRecord;
-
- begin
- {make sure something in the listbox is selected}
- Idx := ListBox1.ItemIndex;
- if (Idx = -1) then begin
- MessageBeep(0);
- Exit;
- end;
-
- {make sure the user really wants to delete the record}
- IniDBase1.GetRecord(ListBox1.Items[Idx], Rec);
- if (MessageDlg('Are you sure you want to delete "' + Rec.Name + '"?',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes) then begin
- {remove the record from the file}
- IniDBase1.DelRecord(ListBox1.Items[Idx]);
-
- {calculate the new position for the highlight bar in the listbox}
- NewIdx := -1;
- if (Idx = Pred(ListBox1.Items.Count)) then begin
- if (Idx <> 0) then
- NewIdx := Pred(Idx);
- end else
- NewIdx := Idx;
-
- {delete the record from the listbox and highlight another record}
- ListBox1.Items.Delete(Idx);
- ListBox1.ItemIndex := NewIdx;
-
- {update the edit components on the form}
- UpdateEdits;
- end;
- end;
-
- procedure TForm1.ListBox1Click(Sender: TObject);
- begin
- UpdateEdits;
- end;
-
- end.
-
-